#coding:utf-8

import pickle
from criet import prpcrypt

from flask import Flask,render_template,request,redirect,session

import user

app = Flask(__name__)

key = '(1(0#5Hb@sa&echo'
'''登录，套用login。html页面'''
@app.route('/')
def index():
    return render_template('login.html')

'''由模版login.html页面超链接到/login/，做处理转发到/pwds/'''
@app.route('/login/', methods=["POST","GET"])
def login():
    params = request.args if request.method == 'GET' else request.form
    password = params.get('password')
    if user.validate_login(password):
        #session['user'] = {'username':username}
        return redirect('/pwds/')
    else:
        return render_template('login.html',error='password error')

# @app.route('/logs/')
# def logs():
#     with open('e:/password.pkl','rb') as f:
#         data = pickle.load(f)
#     return str(data)
'''密码处理，获取所有密码，套用user.html显示'''
@app.route('/pwds/')
def users():
    _users = user.get_pwds()
    _Users = []
    for User in _users:
        l= list(User)
        pc = prpcrypt(key)
        l[4] = pc.decrypt(l[4])
        _Users.append(l)
    #print _Users
    return render_template('user.html',users=_Users)

'''添加密码模块，由user.html转来处理，套用create_password.html模版'''
@app.route('/pwd/add/')
def add_user():
    return render_template('create_user.html')

'''处理添加密码模块，由/pwd/add/模块转来，保存添加的密码，成功转发到/pwds/然后展示所有密码'''
@app.route('/addpwd/',methods=["POST","GET"])
def apppwd():
    params = request.args if request.method == 'GET' else request.form
    tag= params.get('tag')
    title = params.get('title')
    accounts = params.get('accounts')
    telphone = params.get('telphone')
    email = params.get('email')
    pc = prpcrypt(key)
    password = pc.encrypt(params.get('password'))
    list = [tag,title,accounts,password,telphone,email]
    #print list
    if user.get_tag(tag):
        if user.add_pwd(list):
            return redirect('/pwds/')
    else:
        return render_template('create_user.html', error='Type does not exist')



@app.route('/update_pass/',methods=["POST","GET"])
def update_pass():
    params = request.args if request.method == 'GET' else request.form
    id = params.get('id')
    item = user.get_onepwd(id)
    print item
    return render_template('update_pass.html',id=id,tag=item[0],title=item[1],accounts=item[2],telphone=item[4],email=item[5])

@app.route('/update_pwd/',methods=["POST","GET"])
def update_pwd():
    params = request.form
    paget = request.args
    id = paget.get('id')
    tag = params.get('tag')
    title = params.get('title')
    accounts = params.get('accounts')
    telphone = params.get('telphone')
    email = params.get('email')
    pc = prpcrypt(key)
    password = pc.encrypt(params.get('password'))
    tup = [tag, title, accounts, password, telphone, email,int(id)]
    if user.update_pwd(tup):
        return redirect('/pwds/')


@app.route('/delete_pwd/',methods=['GET'])
def delete_pwd():
    params = request.args
    id = params.get('id')
    if user.delete_pwd([id]):
        return redirect('/pwds/')


@app.route('/tag/pwd/',methods=["GET"])
def tag_pwd():
    params = request.args
    id = params.get('id')
    #print id
    _users = user.get_tagpwds(id)
    _Users = []
    for User in _users:
        l = list(User)
        pc = prpcrypt(key)
        l[4] = pc.decrypt(l[4])
        _Users.append(l)
    #print _users
    return render_template('user.html', users=_Users)


@app.route('/tag/main/')
def tag_main():
    _tags = user.get_tags()
    print _tags
    return render_template('tags.html',tags=_tags)

@app.route('/addtag/',methods=["POST","GET"])
def add_tag():
    params = request.args if request.method == 'GET' else request.form
    tag= params.get('tag')
    if user.add_tag([tag]):
        return redirect('/tag/main/')


@app.route('/delete_tag/',methods=['GET'])
def delete_tag():
    params = request.args
    id = params.get('id')
    if user.delete_tag([id]):
        return redirect('/tag/main/')

@app.route('/update_tag/',methods=['GET'])
def update_tag():
    params = request.args
    name= params.get('id')
    item= user.get_tag(name)
    print item
    return render_template('update_tag.html', id=item[0],tag_name=item[1])

@app.route('/update/tag/',methods=['POST','GET'])
def update_Tag():
    id = request.args.get('id')
    name = request.form.get('tag_name')
    list=[name,id]
    tag_before_name = user.get_tag_name(id)
    list1=[name,tag_before_name[0]]
    print list1
    if user.get_tag(name):
        return render_template('update_tag.html', error='Type has been exist',id=id,tag_name=name)
    else:
        if user.update_tag(list):
            user.update_pwd_tag([name,tag_before_name[0]])
            return redirect('/tag/main/')



# @app.route('/pwd/')
# def pwd():
#     params = request.args if request.method == 'GET' else request.form
#     tag = params.get('tag')

app.run(host='0.0.0.0',port=9001,debug=True)
